package com.spring.springbootmodel.controller;

import cn.hutool.core.util.StrUtil;
import com.spring.springbootmodel.common.AuthAccess;
import com.spring.springbootmodel.common.Result;
import com.spring.springbootmodel.entyti.User;
import com.spring.springbootmodel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 作者：黄龙华
 * 功能：提供接口，返回数据
 * 日期：2023/11/2 17:36
 */
@RestController
@RequestMapping("/web")     //一级路由
public class WebController {

    @Autowired
    UserService userService;

    @AuthAccess
    @GetMapping("/")    //二级路由
    public Result Hello() {
        return Result.success("success");
    }

    @AuthAccess
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error("数据不合法");
        }
        user = userService.login(user);
        return Result.success(user);
    }

    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error("数据不合法");
        }
        if (user.getUsername().length() >= 8) {
            return Result.error("账号过长，最多7位");
        }
        if (user.getPassword().length() >= 12) {
            return Result.error("密码过长，最多11位");
        }
        user = userService.register(user);
        return Result.success(user);
    }

}